package com.wanji.gateway.command;

import com.wanji.gateway.constant.CommandPriority;
import com.wanji.gateway.constant.CommandType;

/**
 * 设备指令构建器
 * 使用建造者模式创建复杂的设备指令
 */
public class DeviceCommandBuilder implements CommandBuilder<BaseDeviceCommand> {
    private final BaseDeviceCommand command;

    public DeviceCommandBuilder(CommandType type) {
        command = new BaseDeviceCommand(type);
    }

    @Override
    public BaseDeviceCommand build() {
        return command;
    }

    @Override
    public DeviceCommandBuilder withParam(String key, Object value) {
        command.addParam(key, value);
        return this;
    }

    @Override
    public DeviceCommandBuilder withPriority(CommandPriority priority) {
        command.setPriority(priority);
        return this;
    }

    @Override
    public DeviceCommandBuilder withTimeout(long timeout) {
        command.setTimeout(timeout);
        return this;
    }

    // 便捷方法：创建读取数据指令
    public static DeviceCommandBuilder readDataCommand(String deviceId, String dataPoint) {
        return new DeviceCommandBuilder(CommandType.READ_DEVICE_PARAM)
                .withParam("deviceId", deviceId)
                .withParam("dataPoint", dataPoint)
                .withPriority(CommandPriority.LOW);
    }

    // 便捷方法：创建设置参数指令
    public static DeviceCommandBuilder setParamCommand(String deviceId, String paramKey, Object paramValue) {
        return new DeviceCommandBuilder(CommandType.SET_DEVICE_PARAM)
                .withParam("deviceId", deviceId)
                .withParam("paramKey", paramKey)
                .withParam("paramValue", paramValue)
                .withPriority(CommandPriority.NORMAL);
    }
}
